function domUnload(aEvent) {
  aEvent.target.ownerDocument.removeEventListener("click", domClick, true);
  aEvent.target.ownerDocument.removeEventListener("DOMActivate", domActivate, true);
  aEvent.target.ownerDocument.removeEventListener("unload", domUnload, false);
}

function domClick(aEvent)
{
  var link = aEvent.target;

  if (link instanceof HTMLAnchorElement && 
      link.target != "" &&
      link.target != "_self" &&
      link.target != "_top") {
    aEvent.stopPropagation();
  }
}

function domActivate(aEvent)
{
  var link = aEvent.target;

  if (link instanceof HTMLAnchorElement && 
      link.target != "" &&
      link.target != "_self" &&
      link.target != "_top") {

    // We don't want to open external links in this process: do so in the
    // default browser.
    var ios = Components.classes["@mozilla.org/network/io-service;1"]
                        .getService(Components.interfaces.nsIIOService);

    var resolvedURI = ios.newURI(link.href, null, null);

    var extps = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
                          .getService(Components.interfaces.nsIExternalProtocolService);

    extps.loadURI(resolvedURI, null);
    aEvent.preventDefault();
    aEvent.stopPropagation();
  }
}